﻿using System;
using System.Diagnostics;
using System.IO;
using System.Threading;

namespace DataService.Core.Log
{
    /// <summary> 日志记录</summary>
    public static class TraceLog
    {
        private static readonly object objLock = new object();
        /// <summary>写入日志内容,文本内容自动换行</summary>
        /// <param name="message">文本内容</param>
        public static void WriteLine(string message)
        {
            string tempPath = CreateDirectory("Log");
            if (Monitor.TryEnter(objLock, 1000 * 5))
            {
                tempPath = tempPath + DateTime.Now.ToString("yyyy-MM-dd") + ".txt"; 
                File.AppendAllText(tempPath, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + message + Environment.NewLine);
                Monitor.Exit(objLock);
            }
            else
            {
                tempPath = tempPath + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-fs") + ".txt";
                File.AppendAllText(tempPath, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "发生死锁,牺牲一个锁,参数信息：" + message + Environment.NewLine);
            }

        }
        /// <summary>写入日志内容,文本内容自动换行</summary>
        /// <param name="message">文本内容</param>
        /// <param name="foldName">文件夹名称</param>
        public static void WriteLine(string message, string foldName)
        {
            string tempPath = CreateDirectory(foldName);
            if (Monitor.TryEnter(objLock, 1000 * 5))
            {
                tempPath = tempPath + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                File.AppendAllText(tempPath, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + message + Environment.NewLine);
                Monitor.Exit(objLock);
            }
            else
            {
                tempPath = tempPath + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-fs") + ".txt";
                File.AppendAllText(tempPath, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "发生死锁,牺牲一个锁,参数信息：" + message + Environment.NewLine);
            }

        }
        /// <summary>写入日志内容,文本内容自动换行</summary>
        /// <param name="message">文本内容</param>
        /// <param name="foldName">文件夹名称</param>
        /// <param name="fileName">txt文件名</param>
        public static void WriteLine(string message, string foldName, string fileName)
        {
            string tempPath = CreateDirectory(foldName);
            if (Monitor.TryEnter(objLock, 1000 * 5))
            {
                tempPath = tempPath + fileName + ".txt";
                File.AppendAllText(tempPath, message + Environment.NewLine);
                Monitor.Exit(objLock);
            }
            else
            {
                tempPath = tempPath + fileName + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-fs") + ".txt";
                File.AppendAllText(tempPath, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "发生死锁,牺牲一个锁,参数信息：" + message + Environment.NewLine);
            }

        }
        /// <summary>
        /// 创建文件夹
        /// </summary>
        /// <param name="folderName"></param>
        public static string CreateDirectory(string folderName)
        {
            string tempPath = "";
            string directory = System.Threading.Thread.GetDomain().BaseDirectory;
            if (directory.Contains("Debug"))
            {
                tempPath = directory.Replace("\\bin\\Debug\\", "") + "\\" + folderName + "\\";
            }
            else if (directory.Contains("Release"))
            {
                tempPath = directory.Replace("\\bin\\Release\\", "") + "\\" + folderName + "\\";
            }
            else
            {
                tempPath = directory.Replace("\\bin\\", "") + "\\" + folderName + "\\";
            }
            if (!Directory.Exists(tempPath))
            {
                Directory.CreateDirectory(tempPath);
            }
            return tempPath;
        }

    }
}
